﻿

$(document).ready(function () {
    
    // DevTools page -- devtools.js
    // Create a connection to the background page
    var backgroundPageConnection = chrome.runtime.connect({
        name: "devtools-page"
    });

    backgroundPageConnection.onMessage.addListener(function (data) {

       // alert("devtools: " + JSON.stringify(data));


        $(data.sendMessage.messages).each(function (index) {
             
            if (data.sendMessage.messages[index].type === "info" && 
                data.sendMessage.messages[index].subType == undefined) {
                
                var element = $("<div></div>").loadTemplate("../html/panel.calloutInfoTemplate.html", {
                    W3cTitle: "Info",
                    W3cMessage: data.sendMessage.messages[index].message
                });
               
          
                $("#w3cWrapper").append(element);
              }



            if (data.sendMessage.messages[index].type === "info" && 
                data.sendMessage.messages[index].subType != undefined) {
                 
                var element = $("<div></div>").loadTemplate("../html/panel.calloutWarningTemplate.html", {
                    W3cTitle: "Warnung",
                    W3cMessage: data.sendMessage.messages[index].message
                    
                });

                $("#w3cWrapper").append(element);

            }

            if (data.sendMessage.messages[index].type === "error") {


                var extractText = data.sendMessage.messages[index].extract.replace(/(\r\n|\n|\r)/gm, " ");
                extractText = extractText.replace(/\s+/g, " ").trim();
                extractText = $(extractText).text(extractText);

                var element = $("<div></div>").loadTemplate("../html/panel.calloutErrorTemplate.html", {
                    W3cTitle: "Error",
                    W3cMessage: data.sendMessage.messages[index].message,
                    W3cExtract: extractText,
                    W3cLastLine: data.sendMessage.messages[index].firstColumn,
                    W3cLastColumn: data.sendMessage.messages[index].hiliteStart,
                    W3cFirstColumn: data.sendMessage.messages[index].hiliteLength,
                    W3cHiliteStart: data.sendMessage.messages[index].lastColumn,
                    W3cHiliteLength: data.sendMessage.messages[index].lastLine
                });

                $("#w3cWrapper").append(element);
 
            }
             
        });

         

    });

    backgroundPageConnection.postMessage({
        name: 'init',
        tabId: chrome.devtools.inspectedWindow.tabId
    });


    
});





